package com.hfb.mashibing.alip8.designpattern._02strategy;

import java.util.Arrays;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Main {

    public static void main(String[] args) {
//        int a[] = new int[]{5,6,4,2,3,1,7,5};
//        System.out.println(Arrays.toString(a));
//        Sorter sorter = new Sorter();
//        sorter.sort(a);
//        System.out.println(Arrays.toString(a));


        System.out.println("-------CatComparable----------");
        CatComparable[]c = new CatComparable[]{
            new CatComparable(10),
            new CatComparable(70),
            new CatComparable(30),
            new CatComparable(50)
        };
        System.out.println(Arrays.toString(c));
        Sorter sorter = new Sorter();
        sorter.sort(c);
        System.out.println(Arrays.toString(c));


        System.out.println("-------Cat----------");

        Cat[]c2 = new Cat[]{
            new Cat(12),
            new Cat(72),
            new Cat(32),
            new Cat(52)
        };
        System.out.println(Arrays.toString(c2));
        new Sorter<Cat>().sort(c2, new CatComparator());
        System.out.println(Arrays.toString(c2));

        System.out.println("-------Compartor----------");
        Cat[]c3 = new Cat[]{
            new Cat(13),
            new Cat(73),
            new Cat(33),
            new Cat(53)
        };
        System.out.println(Arrays.toString(c3));
        new Sorter<Cat>().sort(c3, (t1,t2)->{
            if (t1.weight > t2.weight) return 1;
            else if (t1.weight == t2.weight) return 0;
            else return -1;
        });
        System.out.println(Arrays.toString(c3));


    }
}
interface Compartor<T> {
    int compare(T t1, T t2);
}
interface Comparable<T> {
    int compareTo(T o);
}
class Sorter<T> {

    void sort(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i; j < a.length; j++) {
                swap(a, i, j);
            }
        }
    }

    void sort(T[] a, Compartor<T> compartor) {
        for (int i = 0; i < a.length; i++) {
            int minPos = i;
            for (int j = i; j < a.length; j++) {
                minPos = compartor.compare(a[j], a[minPos])==-1 ? j : minPos;
            }
            swap(a, i, minPos);
        }
    }

    private static void swap(Comparable[] a, int i, int j) {
        if (a[i].compareTo(a[j]) > 0) {
            Comparable temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    private void swap(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Cat {
    int weight, height;

    public Cat(int weight) {
        this.weight = weight;
    }

    public int compareTo(Cat o) {
        if (o.weight > this.weight) return 1;
        else if (o.weight == this.weight) return 0;
        else return -1;
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class CatComparable implements Comparable {

    int weight, height;

    public CatComparable(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Object o) {
        CatComparable c = (CatComparable) o;
        if (c.weight > this.weight) {
            return 1;
        } else if (c.weight == this.weight) {
            return 0;
        } else {
            return -1;
        }
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class CatComparator implements Compartor<Cat>{
    int weight, height;

    public CatComparator(int weight) {
        this.weight = weight;
    }

    @Override
    public int compare(Cat t1, Cat t2) {
        if (t1.weight > t2.weight) return 1;
        else if (t1.weight == t2.weight) return 0;
        else return -1;
    }
}
